package Backtracking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _93_SubsetsII {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        int begin = 0;
        res.add(new ArrayList<>());
        for (int i = 0; i < nums.length; i++) {
            if (i == 0 || nums[i] != nums[i - 1]) {
                begin = 0;
            }
            int size = res.size();
            for (int j = begin; j < size; j++) {
                List<Integer> tmp = new ArrayList<>(res.get(j));
                tmp.add(nums[i]);
                res.add(tmp);
            }
            begin = size;
        }
        return res;
    }
}
